# -*- coding: utf-8 -*-

import time
import SimpleHTTPServer
import SocketServer

from StatisticsHolder import *

class HTMLGenerator:
    def init(self):
        pass

    def generateHTML(self):
        t1 = time.clock()

        holder = StatisticsHolder()

        PORT = 8000
        Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
        httpd = SocketServer.TCPServer(("", PORT), Handler)
        print "serving at port", PORT

        ficheiro = file("index.html", "w")

        ficheiro.write("<html><body>")

        self.generateHigh(ficheiro, holder)
        self.generateCategory(ficheiro, holder)
        self.generateDegree(ficheiro, holder)
        
        ficheiro.write("</html></body>")

        ficheiro.close()

        print "Server Ready"

        t2 = time.clock()
        print "HTML Generation = ", t2-t1

        httpd.serve_forever()        
        pass
    
    def generateHigh(self, ficheiro, holder):
        t1 = time.clock()
        ficheiro.write("<h1>Teachers per establishment per year</h1>")

        stats = holder.listHighTeachers()

        for year in sorted(stats.iterkeys()):
            ficheiro.write("<h2>{0}</h2>".format(year))

            for est in sorted(stats.get(year).iterkeys()):
                ficheiro.write("<h3>{0}</h3>".format(est.encode("iso-8859-1")))

                ficheiro.write("<ul>")

                for x in stats.get(year).get(est):
                    ficheiro.write("<li>" + x.encode("iso-8859-1") + "</li>")
                    pass
                
                ficheiro.write("</ul>")
                pass
            pass
##            break

        self.generateHighCSV(stats)
        
        t2 = time.clock()
        print "HTML High =", t2-t1
        return ficheiro

    def generateHighCSV(self, stats):
        t1 = time.clock()

        writer = csv.writer(open("./csv/ListHighTeachers.csv", "wb"))

        writer.writerow(["Year", "Establishment", "Name"])

        for year in sorted(stats.iterkeys()):
            for est in sorted(stats.get(year).iterkeys()):
                for x in sorted(stats.get(year).get(est)):
                    writer.writerow([year.encode("iso-8859-1"),\
                                     est.encode("iso-8859-1"),\
                                     x.encode("iso-8859-1")])
                    pass
                pass
            pass

        t2 = time.clock()
        print "HTML High CSV =", t2-t1
        pass

    def generateCategory(self, ficheiro, holder):
        t1 = time.clock()
        ficheiro.write("<h1>Teachers divided per categories\
                        per establisment and per year</h1>")

        stats = holder.listCategoryTeachers()

        for year in sorted(stats.iterkeys()):
            ficheiro.write("<h2>{0}</h2>".format(year))

            for est in sorted(stats.get(year).iterkeys()):
                ficheiro.write("<h3>{0}</h3>".format(est.encode("iso-8859-1")))

                for cat in sorted(stats.get(year).get(est).iterkeys()):
                    
                    ficheiro.write("<h4>{0}</h4>".format(cat.encode("iso-8859-1")))

                    ficheiro.write("<ul>")

                    for x in stats.get(year).get(est).get(cat):
                        ficheiro.write("<li>" + x.encode("iso-8859-1") + "</li>")
                        pass
                
                    ficheiro.write("</ul>")
                    pass
                pass
            pass
##            break

        self.generateCategoryCSV(stats)
        
        t2 = time.clock()
        print "HTML Category =", t2-t1            
        return ficheiro

    def generateCategoryCSV(self, stats):
        t1 = time.clock()

        writer = csv.writer(open("./csv/ListCategoryTeachers.csv", "wb"))

        writer.writerow(["Year", "Establishment", "Category",\
                         "Name"])

        for year in sorted(stats.iterkeys()):
            for est in sorted(stats.get(year).iterkeys()):
                for cat in sorted(stats.get(year).get(est).iterkeys()):
                    for x in stats.get(year).get(est).get(cat):
                        writer.writerow([year.encode("iso-8859-1"),\
                                         est.encode("iso-8859-1"),\
                                         cat.encode("iso-8859-1"),\
                                         x.encode("iso-8859-1")])
                    pass
                pass
            pass

        t2 = time.clock()
        print "HTML Category CSV =", t2-t1
        pass
    
    def generateDegree(self, ficheiro, holder):
        t1 = time.clock()        
        ficheiro.write("<h1>Teachers divided per type of degree\
                        (doctorate level, masters level and\
                        graduate level) per establishment and per year</h1>")

        stats = holder.listDegreeTeachers()

        for year in sorted(stats.iterkeys()):
            ficheiro.write("<h2>{0}</h2>".format(year))

            for est in sorted(stats.get(year).iterkeys()):
                ficheiro.write("<h3>{0}</h3>".format(est.encode("iso-8859-1")))

                for deg in sorted(stats.get(year).get(est).iterkeys()):
                    
                    ficheiro.write("<h4>{0}</h4>".format(deg.encode("iso-8859-1")))

                    ficheiro.write("<ul>")

                    for x in stats.get(year).get(est).get(deg):
                        ficheiro.write("<li>" + x.encode("iso-8859-1") + "</li>")
                        pass
                
                    ficheiro.write("</ul>")
                    pass
                pass
            pass
##            break

        self.generateDegreeCSV(stats)

        t2 = time.clock()
        print "HTML Degree =", t2-t1            
        return ficheiro

    def generateDegreeCSV(self, stats):
        t1 = time.clock()

        writer = csv.writer(open("./csv/ListDegreeTeachers.csv", "wb"))

        writer.writerow(["Year", "Establishment", "Degree",\
                         "Name"])

        for year in sorted(stats.iterkeys()):
            for est in sorted(stats.get(year).iterkeys()):
                for deg in sorted(stats.get(year).get(est).iterkeys()):
                    for x in stats.get(year).get(est).get(deg):
                        writer.writerow([year.encode("iso-8859-1"),\
                                         est.encode("iso-8859-1"),\
                                         deg.encode("iso-8859-1"),\
                                         x.encode("iso-8859-1")])
                    pass
                pass
            pass

        t2 = time.clock()
        print "HTML Degree CSV =", t2-t1
        pass

##gen = HTMLGenerator().generateHTML()
